/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information:
 *
 *  Last Modified On: $Date: 2007-06-04 21:19:39 +0000 (Mon, 04 Jun 2007) $
 *  Last Modified By: $Author: stefan.bucur $
 */

package ro.pub.cs.stadium.platform;

import org.eclipse.swt.widgets.Display;

import ro.pub.cs.stadium.document.IDocument;
import ro.pub.cs.stadium.document.logic.Document;
import ro.pub.cs.stadium.testing.BasicDocument;

public class StadiumApplication{
	
	/**
	 * The directory name of Stadium settings
	 */
	private final String stadiumDir = ".stadium";
	private final String settingsFile = "settings/settings.xml";
	
	private Display appDisplay = null;
	private MainWindow mainWindow = null;
	
	private static StadiumApplication theApplication = null;
	
	/**
	 * Performs the application initialization
	 * @author stefan
	 *
	 */
	private class AppLoader implements ISplashRunnable{
		
		private final int MIN_SPLASH_TIME = 1000; //millisecs

		public void run(SplashScreen splash) {
			try {
				splash.setProgressMax(100);
				
				splash.setProgress(0, "Initializing");
				
				appDisplay.asyncExec(new Runnable() {
					public void run() {
						createMainShell();
					}
				});
				
				splash.setProgress(100, "Done");
				Thread.sleep(MIN_SPLASH_TIME);
				
				appDisplay.asyncExec(new Runnable() {
					public void run() {
						mainWindow.getShell().open();
						
						mainWindow.loadDocument(new Document());
					}
				});
				
			} catch (InterruptedException ex) {
				
			}
		}

	}
	
	private void createMainShell() {
		mainWindow = new MainWindow(appDisplay);
	}
	
	public StadiumApplication() {
		appDisplay = new Display();
		
		
	}
	
	/**
	 * Initializes the component loading
	 *
	 */
	public void initialize() {
		SplashScreen splashScreen = new SplashScreen(appDisplay);
		splashScreen.runSplash(new AppLoader());
	}
	
	/**
	 * The main SWT loop
	 *
	 */
	public void run() {
		
		while (true) {
			if (mainWindow != null && mainWindow.getShell().isDisposed())
				break;
			
			if (!appDisplay.readAndDispatch()) {
				appDisplay.sleep();
			}
			
		}
	}
	
	public Display getDisplay() {
		return appDisplay;
	}
	
	public MainWindow getMainShell() {
		return mainWindow;
	}
	
	/**
	 * Disposes all the application resources
	 *
	 */
	public void dispose() {
		appDisplay.dispose();
	}
	
	/**
	 * 
	 * @return a string representing the user specific path
	 * used to store settings and any other user data
	 */
	public String getUserStadiumPath() {
		return System.getProperty("user.home") + "/" + stadiumDir + "/";
	}
	
	public String getGlobalStadiumPath() {
		return System.getProperty("user.dir") + "/";
	}
	
	/**
	 * 
	 * @return the name of the user specific settings file
	 */
	public String getUserSettingsFile() {
		return getUserStadiumPath() + settingsFile;
	}
	
	/**
	 * 
	 * @return the name of the global settings file
	 */
	public String getGlobalSettingsFile() {
		return getGlobalStadiumPath() + settingsFile;
	}
	
	public static StadiumApplication getApplication() {
		return theApplication;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		theApplication = new StadiumApplication();
		
		theApplication.initialize();
		
		theApplication.run();
		
		theApplication.dispose();
	}

}
